﻿using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Samurai.Wakizashi.Workflow;

namespace Samurai.Workflow.Tests
{
    [TestClass]
    public class Parallel_Workflow
    {
        [TestMethod]
        public void Executes_registered_workflows_asynchronously()
        {
            var initialThreadCount = Process.GetCurrentProcess().Threads.Count;

            var testWf1 = new SequentialWorkflow<TestWorkflowContext>();
            testWf1.RegisterStep(new BlockingWorkflowStep(500));

            var testWf2 = new SequentialWorkflow<TestWorkflowContext>();
            testWf2.RegisterStep(new BlockingWorkflowStep(500));

            var pWf = new ParallelWorkflow<TestWorkflowContext>();
            pWf.RegisterWorkflow(testWf1);
            pWf.RegisterWorkflow(testWf2);
            pWf.Start();

            Assert.IsTrue(Process.GetCurrentProcess().Threads.Count == (initialThreadCount + 2));
        }
    }

    internal class BlockingWorkflowStep : WorkflowStepBase<TestWorkflowContext>
    {
        private readonly int _secondsToBlock;

        public BlockingWorkflowStep(int secondsToBlock)
        {
            _secondsToBlock = secondsToBlock;
        }

        public override void ExecuteStep()
        {
            System.Threading.Thread.Sleep(_secondsToBlock);
        }
    }
}
